<template>
  <div>
    <h2>我是 Food 各地美食的内容</h2>
    <h3 :style="{ opacity }">一闪一闪亮晶晶</h3>
  </div>
</template>

<script>
export default {
  name: "Food",
  data() {
    return {
      opacity: 1,
    };
  },
  activated() {
    console.log("Food 组件被激活了");
    this.timer = setInterval(() => {
      console.log("定时器被执行");
      this.opacity -= 0.01;
      if (this.opacity <= 0) {
        this.opacity = 1;
      }
    }, 16);
  },
  deactivated() {
    console.log("Food 组件失活了");
    clearInterval(this.timer);
  },
  //通过路由规则，进入该组件时被调用
  beforeRouteEnter(to, from, next) {
    console.log("组件内路由守卫:Food--beforeRouteEnter", to, from);
    if (to.meta.isAuth) {
      //判断是否需要鉴权
      if (localStorage.getItem("userName") === "流放深圳") {
        next();
      } else {
        alert("Sorry，用户名不正确，你无权查看此页面！");
      }
    } else {
      next();
    }
  },
  //通过路由规则，离开该组件时被调用
  beforeRouteLeave(to, from, next) {
    console.log("组件内路由守卫:Food--beforeRouteLeave", to, from);
    next();
  },
};
</script>